package com.zhongfl.crm.lixiao.common;

import com.heptagram.lixiao.api.LiXiaoService;
import com.heptagram.lixiao.bean.request.contract.LXContractListRequest;
import com.heptagram.lixiao.bean.request.opportunity.LXOpportunityListRequest;
import com.heptagram.lixiao.bean.response.contract.LXContractListResponse;
import com.heptagram.lixiao.bean.response.opportunity.LXOpportunityListResponse;
import com.heptagram.lixiao.bean.response.opportunity.LXOpportunityResponse;
import com.heptagram.lixiao.common.bean.Order;
import com.heptagram.lixiao.common.exception.LiXiaoErrorException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * LxOrderListUtils 励销获取订单列表工具类
 *
 * @author tiantian
 * @date 2018/8/30 14:53
 */
@Service
@Slf4j
public class LxOrderListUtilsCRM {

    @Resource
    private LiXiaoService liXiaoService;

    /**
     * 查询条件
     */
    private static final int PAGE_SIZE = 10;


    /**
     * 查询客户订单列表
     *
     * @param pageIndex 页码
     * @param sortMode  排序字段
     * @return 励销机会列表
     * @throws LiXiaoErrorException 励销异常
     */
    public LXOpportunityListResponse findOpportunities(int pageIndex, String sortMode) throws LiXiaoErrorException {
        return findOpportunities(pageIndex, sortMode, PAGE_SIZE);
    }

    public LXOpportunityListResponse findOpportunities(int pageIndex, String sortMode, int pageSize) throws LiXiaoErrorException {
        LXOpportunityListRequest lxOpportunityListRequest = new LXOpportunityListRequest();
        lxOpportunityListRequest.setPageIndex(pageIndex);
        lxOpportunityListRequest.setPageSize(pageSize);
        lxOpportunityListRequest.setSort(sortMode);
        lxOpportunityListRequest.setOrder(Order.DESC);

        return findOpportunities(lxOpportunityListRequest);
    }

    /**
     * 查询客户订单列表
     *
     * @param lxOpportunityListRequest 请求参数
     * @return 励销机会列表
     * @throws LiXiaoErrorException 励销异常
     */
    private LXOpportunityListResponse findOpportunities(LXOpportunityListRequest lxOpportunityListRequest) throws LiXiaoErrorException {
        LXOpportunityListResponse opportunitiesResponse = liXiaoService.getOpportunityService().findOpportunities(lxOpportunityListRequest);

        if (Objects.isNull(opportunitiesResponse)) {
            log.info("没有查询到客户订单记录");
            return null;
        }

        if (opportunitiesResponse.getData().getTotalCount() == 0) {
            log.info("没有查询到客户订单记录");
            return null;
        }
        return opportunitiesResponse;
    }

    public LXOpportunityResponse.DataResult findOpportunityById(Long id) throws LiXiaoErrorException {
        LXOpportunityResponse opportunityResponse = liXiaoService.getOpportunityService().findOpportunityByOrderId(id);
        return opportunityResponse.getData();
    }

    /**
     * 查询交付订单列表
     *
     * @param pageIndex 页码
     * @param sortMode  排序字段
     * @return 励销合同列表
     * @throws LiXiaoErrorException 励销异常
     */
    public LXContractListResponse findContracts(int pageIndex, String sortMode) throws LiXiaoErrorException {
        LXContractListRequest lxContractListRequest = new LXContractListRequest();
        lxContractListRequest.setPageIndex(pageIndex);
        lxContractListRequest.setPageSize(PAGE_SIZE);
        lxContractListRequest.setSort(sortMode);
        lxContractListRequest.setOrder(Order.DESC);

        return findContracts(lxContractListRequest);
    }

    /**
     * 查询交付订单列表
     *
     * @param lxContractListRequest 请求参数
     * @return 励销合同列表
     * @throws LiXiaoErrorException 励销异常
     */
    private LXContractListResponse findContracts(LXContractListRequest lxContractListRequest) throws LiXiaoErrorException {
        LXContractListResponse contractsResponse = liXiaoService.getContractService().findContracts(lxContractListRequest);

        if (Objects.isNull(contractsResponse)) {
            log.info("没有查询到交付订单记录");
            return null;
        }

        if (contractsResponse.getData().getTotalCount() == 0) {
            log.info("没有查询到交付订单记录");
            return null;
        }
        return contractsResponse;
    }
}
